$(function() {
	// editor
	var editor = new wangEditor('content-field');
    editor.config.uploadImgUrl = '/bd/upload';
    editor.create();
    window.editor = editor;

	// selectize
	selectize();

	if(window.menuId) {
		fillForm(window.menuId);
	}

	// add events
	$('.btn-submit').on('click', onSubmit);
}());

function selectize() {
	window.$mainSelect = $('.main-staff').selectize({
		delimiter: ',',
		valueField: 'value',
		labelField: 'text',
		create: function(input) {
			return {
				value: input,
				text: input
			}
		}
	});
	window.$assistSelect = $('.assist-staff').selectize({
		delimiter: ',',
		valueField: 'value',
		labelField: 'text',
		create: function(input) {
			return {
				value: input,
				text: input
			}
		}
	});

	window.$mainSelect = window.$mainSelect[0].selectize;
	window.$assistSelect = window.$assistSelect[0].selectize;
}

function fillForm(id) {
	ghttp.get('/bd/menu/' + id + '/data', null, function(resp) {
		if(resp.success) {
			var result = resp.result;
			$('input[name=title]').val(result.title);
			$('textarea[name=summary]').val(result.summary);
			$('.thumbnail-path a').attr('href', result.thumbnail);
			$('.thumbnail-path a').text(result.thumbnail);
			$('#thumbnail_org').val(result.thumbnail);
			$('.thumbnail-path').show();

			if(result.status == 2) {
				$($('.status input').get(1)).attr('checked', 'checked');
			} else {
				$($('.status input').get(0)).attr('checked', 'checked');
			}

			if(result.isRecommend == 1) {
				$($('.isRecommend input').get(1)).attr('checked', 'checked');
			} else {
				$($('.isRecommend input').get(0)).attr('checked', 'checked');
			}
			
			window.editor.$txt.html(result.content);

			// 主料
			var mains = result.main;
			if(mains) {
				mains = mains.split(',');
				mains.forEach(function(item) {
					window.$mainSelect.addOption({ text:item, value: item });
					window.$mainSelect.addItem(item);
				});
			}

			// 辅料
			var assists = result.assist;
			if(assists) {
				assists = assists.split(',');
				assists.forEach(function(item) {
					window.$assistSelect.addOption({ text:item, value: item });
					window.$assistSelect.addItem(item);
				});
			}
		}
	});
}

function passed() {
	var title = $('input[name=title]').val();
	var content = $('textarea[name=content]').val();
	var file = $('input[name=thumbnail]').get(0).files[0];
	var main = window.$mainSelect.getValue();
	var assist = window.$assistSelect.getValue();

	if(!title) {
		alert('请输入标题');
		return false;
	}

	if(!content) {
		alert('请输入内容');
		return false;
	}

	if(!file || file.size === 0) {
		if(!window.menuId) {
			alert('请上传图片');
			return false;
		}
	}

	if(!main) {
		alert('请填写主料');
		return false;
	}
	if(!assist) {
		alert('请填写辅料');
		return false;
	}

	return true;
}

function onSubmit() {
	var url, method;
	if(passed()) {
		var formData = new FormData($('form')[0]);
		url = '/bd/menu';
		method = 'POST';
		if(window.menuId) {
			url = '/bd/menu/' + window.menuId;
			method = 'POST';
		}
	    $.ajax({
	        url: url,  //Server script to process data
	        type: method,
	        // xhr: function() {  // Custom XMLHttpRequest
	        //     var myXhr = $.ajaxSettings.xhr();
	        //     if(myXhr.upload){ // Check if upload property exists
	        //         myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
	        //     }
	        //     return myXhr;
	        // },
	        //Ajax events
	        // beforeSend: beforeSendHandler,
	        success: function(resp) {
	        	if(!resp.success) {
	        		alert(resp.result);
	        		return;
	        	}
	        	window.location.href = '/bd/menu';
	        },
	        error: function() {

	        },
	        // Form data
	        data: formData,
	        //Options to tell jQuery not to process data or worry about content-type.
	        cache: false,
	        contentType: false,
	        processData: false
	    });
	}
}